<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 呼叫系统：个人通话管理
 */

class CallController extends BaseController {
    //获取通话类型
    public function calltypes(){
        $res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
        $MCalllog = D("Calllog");
        $res['data'] = $MCalllog->getTypes();
        e_json( $res );
    }

    //时长统计:获取今日、全部的通话时长统计信息
	public function timeStatistics(){
		$s_key = I('get.key','','trim');
		$s_begintime = I('get.begintime','','trim');
		$s_endtime = I('get.endtime','','trim');
		$s_type = I('get.type','', 'intval');
		$this_month = date('Ym');
		$MCalllog = D("Calllog");
		$wheres = array();
        $map = array();
		$wheres['_string'] = '1=1';
		$wheres['admin_no'] = array('eq',$this->m_user['user_id']);
		if( $s_type && $s_type != "7" )
			$wheres['type'] = array('eq', $s_type-1 );
        else if($s_type && $s_type == "7")   //筛选通话类型为全部
            $wheres['type'] = array('in',array('0','1','2','3','5'));
		if($s_key == "today")             //今日时长统计
            $wheres['calldate'] = array('eq',date('Ymd'));
        else if($s_key == "month")         //本月时长统计
        	$wheres['calldate'] = array('like','%'.$this_month.'%');
        else if("week" == $s_key )         //本周时长统计
            $wheres['calldate'] = array('between',$MCalllog->getWeek());
        if($s_begintime && $s_endtime && $s_begintime < $s_endtime)
        	$wheres['calldate'] = array('between',array(str_replace('-','',$s_begintime),str_replace('-','',$s_endtime)));    //去掉日期间-(2016-10-09处理成20191009)
        else if($s_begintime && $s_endtime && strtotime($s_begintime) == strtotime($s_endtime))  //开始时间=结束时间则处理成该天时长统计
            $wheres['calldate'] = array('eq',str_replace('-','',$s_begintime));
        $res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
        //通话次数
        $cal_times = $MCalllog->where($wheres) -> count();
        $RR = $wheres;
        $RR['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times = $MCalllog->where($RR) -> count();


    //计算总通话时长信息
        $call_list = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($wheres)
                     ->order("lasttime desc ")
                     ->select();
        //0打入
        $where1 = $wheres;
        $where1['type'] = array('eq','0');
        $call_list1 = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($where1)
                     ->order("lasttime desc ")
                     ->select();
        $where11 = $where1;
        $where11['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times1 = $MCalllog->where($where11) -> count();

        //1打出
        $where2 = $wheres;
        $where2['type'] = array('eq','1');
        $call_list2 = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($where2)
                     ->order("lasttime desc ")
                     ->select();
        $where22 = $where2;
        $where22['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times2 = $MCalllog->where($where22) -> count();

        //2转入
        $where3 = $wheres;
        $where3['type'] = array('eq','2');
        $call_list3 = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($where3)
                     ->order("lasttime desc ")
                     ->select();
        $where33 = $where3;
        $where33['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times3 = $MCalllog->where($where33) -> count();
        //3转出
        $where4 = $wheres;
        $where4['type'] = array('eq','3');
        $call_list4 = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($where4)
                     ->order("lasttime desc ")
                     ->select();
        $where44 = $where4;
        $where44['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times4 = $MCalllog->where($where44) -> count();
        //双向外呼
        $where5 = $wheres;
        $where5['type'] = array('eq','5');
        $call_list5 = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($where5)
                     ->order("lasttime desc ")
                     ->select();
        $where55 = $where5;
        $where55['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times5 = $MCalllog->where($where55) -> count();

        if($call_list){
            foreach ($call_list1 as &$call) { /*0打入*/
                    $callinfo_show_arr1['dial_cal_time'] = 0;         //拨打电话：dial_cal_time
                    $callinfo_show_arr1['dial_cal_time_show'] = 0;         //拨打电话：dial_cal_time
                    $callinfo_show_arr1['anser_cal_time'] += $call['calltime']; //累计接听电话:anser_cal_time
                    $callinfo_show_arr1['anser_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['anser_cal_time']); //累计接听电话:anser_cal_time转换为时分秒格式
                    $callinfo_show_arr1['cal_time'] += $call['calltime']; //通话时长:cal_time
                    $callinfo_show_arr1['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['cal_time']); //通话时长:cal_time转换为时分秒格式
                    $callinfo_show_arr1['per_cal_time'] = floor($callinfo_show_arr1['cal_time']/$eff_cal_times1);//每通话电话时长:per_cal_time
                    $callinfo_show_arr1['per_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['per_cal_time']);//每通话电话时长:per_cal_time
                }
               foreach ($call_list2 as &$call) {  /*1打出*/
                    $callinfo_show_arr2['dial_cal_time'] += (strtotime($call['endtime'])-strtotime($call['begintime']));         //拨打电话：dial_cal_time
                    $callinfo_show_arr2['dial_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr2['dial_cal_time']); //累计接听电话:anser_cal_time转换为时分秒格式
                    $callinfo_show_arr2['anser_cal_time'] = 0; //累计接听电话:anser_cal_time
                    $callinfo_show_arr2['anser_cal_time_show'] = 0;
                    $callinfo_show_arr2['cal_time'] += $call['calltime']; //通话时长:cal_time
                    $callinfo_show_arr2['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr2['cal_time']); //通话时长:cal_time转换为时分秒格式
                    $callinfo_show_arr2['per_cal_time'] = floor($callinfo_show_arr2['cal_time']/$eff_cal_times2);//每通话电话时长:per_cal_time
                    $callinfo_show_arr2['per_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr2['per_cal_time']);//每通话电话时长:per_cal_time
                }
                foreach ($call_list3 as &$call) {  /*2呼叫转移入*/
                    $callinfo_show_arr3['dial_cal_time'] = 0;         //拨打电话：dial_cal_time
                    $callinfo_show_arr3['dial_cal_time_show'] = 0;
                    $callinfo_show_arr3['anser_cal_time'] += $call['calltime']; //累计接听电话:anser_cal_time
                    $callinfo_show_arr3['anser_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr3['anser_cal_time']); //累计接听电话:anser_cal_time转换为时分秒格式
                    $callinfo_show_arr3['cal_time'] += $call['calltime']; //通话时长:cal_time
                    $callinfo_show_arr3['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr3['cal_time']); //通话时长:cal_time转换为时分秒格式
                    $callinfo_show_arr3['per_cal_time'] = floor($callinfo_show_arr3['cal_time']/$eff_cal_times3);//每通话电话时长:per_cal_time
                    $callinfo_show_arr3['per_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr3['per_cal_time']);//每通话电话时长:per_cal_time
                }
                foreach ($call_list4 as &$call) {  /*3呼叫转移出*/
                    $callinfo_show_arr4['dial_cal_time'] += $call['calltime'];         //拨打电话：dial_cal_time
                    $callinfo_show_arr4['dial_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr4['dial_cal_time']); //累计接听电话:anser_cal_time转换为时分秒格式
                    $callinfo_show_arr4['anser_cal_time'] = 0; //累计接听电话:anser_cal_time
                    $callinfo_show_arr4['anser_cal_time_show'] = 0;
                    $callinfo_show_arr4['cal_time'] += $call['calltime']; //通话时长:cal_time
                    $callinfo_show_arr4['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr4['cal_time']); //通话时长:cal_time转换为时分秒格式
                    $callinfo_show_arr4['per_cal_time'] = floor($callinfo_show_arr4['cal_time']/$eff_cal_times4);//每通话电话时长:per_cal_time
                    $callinfo_show_arr4['per_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr4['per_cal_time']);//每通话电话时长:per_cal_time
                }
                foreach ($call_list5 as &$call) {  /*5双向外呼*/
                    $callinfo_show_arr5['dial_cal_time'] += $call['calltime'];         //拨打电话：dial_cal_time
                    $callinfo_show_arr5['dial_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr5['dial_cal_time']); //累计接听电话:anser_cal_time转换为时分秒格式
                    $callinfo_show_arr5['anser_cal_time'] = 0; //累计接听电话:anser_cal_time
                    $callinfo_show_arr5['anser_cal_time_show'] = 0;
                    $callinfo_show_arr5['cal_time'] += $call['calltime']; //通话时长:cal_time
                    $callinfo_show_arr5['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr5['cal_time']); //通话时长:cal_time转换为时分秒格式
                    $callinfo_show_arr5['per_cal_time'] = floor($callinfo_show_arr5['cal_time']/$eff_cal_times5);//每通话电话时长:per_cal_time
                    $callinfo_show_arr5['per_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr5['per_cal_time']);//每通话电话时长:per_cal_time
                }
                //拨打电话：dial_cal_time_show
                $callinfo_show_arr6['dial_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['dial_cal_time']+$callinfo_show_arr2['dial_cal_time']
                                                +$callinfo_show_arr3['dial_cal_time']+$callinfo_show_arr4['dial_cal_time']+$callinfo_show_arr5['dial_cal_time']);
                //累计接听电话:anser_cal_time_show
                $callinfo_show_arr6['anser_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['anser_cal_time']+$callinfo_show_arr2['anser_cal_time']
                                                +$callinfo_show_arr3['anser_cal_time']+$callinfo_show_arr4['anser_cal_time']+$callinfo_show_arr5['anser_cal_time']);
                //通话时长：cal_time_show
                $callinfo_show_arr6['cal_time'] = $callinfo_show_arr1['cal_time']+$callinfo_show_arr2['cal_time']
                                                +$callinfo_show_arr3['cal_time']+$callinfo_show_arr4['cal_time']+$callinfo_show_arr5['cal_time'];
                $callinfo_show_arr6['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['cal_time']+$callinfo_show_arr2['cal_time']
                                                +$callinfo_show_arr3['cal_time']+$callinfo_show_arr4['cal_time']+$callinfo_show_arr5['cal_time']);
                $callinfo_show_arr6['per_cal_time_show'] = $MCalllog->Sec2Time(floor($callinfo_show_arr6['cal_time']/$eff_cal_times));//每通话电话时长:per_cal_time

        }
    if($cal_times){
        $res['type'] = $s_type;
        $res['key'] = $s_key;
        if($s_type == "1")      /*0打入*/
            $res['callinfo_show'] = $callinfo_show_arr1;
        else if($s_type == "2") /*1打出*/
            $res['callinfo_show'] = $callinfo_show_arr2;
        else if($s_type =="3") /*2呼叫转移入*/
            $res['callinfo_show'] = $callinfo_show_arr3;
        else if($s_type == "4") /*3呼叫转移出*/
            $res['callinfo_show'] = $callinfo_show_arr4;
        else if($s_type == "6") /*5双向外呼*/
            $res['callinfo_show'] = $callinfo_show_arr5;
        else $res['callinfo_show'] = $callinfo_show_arr6;
        $res['call_list'] = $call_list;
		$res['cal_times'] = $cal_times;               //通话次数
        $res['eff_cal_times'] = $eff_cal_times;       //有效通话次数
		$res['where'] = $wheres;
        $res['map'] = $map;
		$res['sql'] = $sql;
		$res['rcode'] = 1;
    }else{
        $res['where'] = $wheres;
        $res['sql'] = $sql;
        $res['rcode'] = 2;
    }
		e_json( $res );
	}

    //时长统计：获取本周、本月或者某时间段内通话时长统计记录列表
    public function getChangeTrends(){
        $psize = I('get.psize', $this->m_psize,'intval');
        $s_key = I('get.key','','trim');
        $s_begintime = I('get.begintime','','trim');
        $s_endtime = I('get.endtime','','trim');
        $s_type = I('get.type','', 'intval');

        if( !$psize )
            $psize = $this->m_psize;
        else if( $psize > 20 )
            $psize = $this->m_psize;

        $wheres = array();
        $map = array();
        $RR = array();
        $this_month = date('Ym');
        $MCalllog = D("Calllog");
        $wheres['admin_no'] = array('eq',$this->m_user['user_id']);
        if( $s_type && $s_type != "7" )
            $wheres['type'] = array('eq', $s_type-1 );
        else if($s_type && $s_type == "7")   //筛选通话类型为全部
            $wheres['type'] = array('in',array('0','1','2','3','5'));
        if($s_key == "month"){
            //本月时长统计
            $wheres['calldate'] = array('like','%'.$this_month.'%');
            $month_shows = $MCalllog->getMonthDay(date());   //获取本周起止时间(年月日之间没有-)
            for($i=intval($month_shows['month_start_day']);$i<=intval($month_shows['month_end_day']);$i++){
                $month_show[] = $i;             //获取本月每天的时间
                if(substr(strval($i),6,2)<'10')  $month_show_2[] = substr(strval($i),7,1);
                else $month_show_2[] = substr(strval($i),6,2);
            }
        }
        else if("week" == $s_key ){
            //本周时长统计
            $wheres['calldate'] = array('between',$MCalllog->getWeek());
            $week_shows = $MCalllog->getWeekDay("addok");   //本周每天时间中存在-，即20161012为2016-10-12
            $week_show = $MCalllog->getWeekDay('addno');   //本周每天时间中不存在-
        }
        if($s_begintime && $s_endtime && $s_begintime < $s_endtime){
            //某时间段时长统计
            $wheres['calldate'] = array('between',array(str_replace('-','',$s_begintime),str_replace('-','',$s_endtime))); //去掉日期间-(2016-10-09处理成20161009)
        }else if($s_begintime && $s_endtime && strtotime($s_begintime) == strtotime($s_endtime))  //开始时间=结束时间则处理成该天通话信息
            $wheres['calldate'] = array('eq',str_replace('-','',$s_begintime));

        $res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
        //通话次数
        $cal_times = $MCalllog->where($wheres) -> count();
        $RR = $wheres;
        $RR['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times = $MCalllog->where($RR) -> count();

    //计算总通话时长信息
        $call_list = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($wheres)
                     ->order("lasttime desc ")
                     ->select();
        //0打入
        $where1 = $wheres;
        $where1['type'] = array('eq','0');
        $call_list1 = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($where1)
                     ->order("lasttime desc ")
                     ->select();
        $where11 = $where1;
        $where11['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times1 = $MCalllog->where($where11) -> count();

        //1打出
        $where2 = $wheres;
        $where2['type'] = array('eq','1');
        $call_list2 = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($where2)
                     ->order("lasttime desc ")
                     ->select();
        $where22 = $where2;
        $where22['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times2 = $MCalllog->where($where22) -> count();

        //2转入
        $where3 = $wheres;
        $where3['type'] = array('eq','2');
        $call_list3 = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($where3)
                     ->order("lasttime desc ")
                     ->select();
        $where33 = $where3;
        $where33['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times3 = $MCalllog->where($where33) -> count();
        //3转出
        $where4 = $wheres;
        $where4['type'] = array('eq','3');
        $call_list4 = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($where4)
                     ->order("lasttime desc ")
                     ->select();
        $where44 = $where4;
        $where44['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times4 = $MCalllog->where($where44) -> count();
        //双向外呼
        $where5 = $wheres;
        $where5['type'] = array('eq','5');
        $call_list5 = $MCalllog
                     ->field("begintime,endtime,calltime,type,calldate")
                     ->where($where5)
                     ->order("lasttime desc ")
                     ->select();
        $where55 = $where5;
        $where55['calltime'] = array('neq',0);
        //有效通话次数
        $eff_cal_times5 = $MCalllog->where($where55) -> count();
        $sql = $MCalllog -> getLastSql();
        if($call_list){
            foreach ($call_list1 as &$call) { /*0打入*/
                    $callinfo_show_arr1['dial_cal_time'] = 0;         //拨打电话：dial_cal_time
                    $callinfo_show_arr1['dial_cal_time_show'] = 0;         //拨打电话：dial_cal_time
                    $callinfo_show_arr1['anser_cal_time'] += $call['calltime']; //累计接听电话:anser_cal_time
                    $callinfo_show_arr1['anser_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['anser_cal_time']); //累计接听电话:anser_cal_time转换为时分秒格式
                    $callinfo_show_arr1['cal_time'] += $call['calltime']; //通话时长:cal_time
                    $callinfo_show_arr1['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['cal_time']); //通话时长:cal_time转换为时分秒格式
                    $callinfo_show_arr1['per_cal_time'] = floor($callinfo_show_arr1['cal_time']/$eff_cal_times1);//每通话电话时长:per_cal_time
                    $callinfo_show_arr1['per_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['per_cal_time']);//每通话电话时长:per_cal_time
                }
               foreach ($call_list2 as &$call) {  /*1打出*/
                    $callinfo_show_arr2['dial_cal_time'] += (strtotime($call['endtime'])-strtotime($call['begintime']));         //拨打电话：dial_cal_time
                    $callinfo_show_arr2['dial_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr2['dial_cal_time']); //累计接听电话:anser_cal_time转换为时分秒格式
                    $callinfo_show_arr2['anser_cal_time'] = 0; //累计接听电话:anser_cal_time
                    $callinfo_show_arr2['anser_cal_time_show'] = 0;
                    $callinfo_show_arr2['cal_time'] += $call['calltime']; //通话时长:cal_time
                    $callinfo_show_arr2['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr2['cal_time']); //通话时长:cal_time转换为时分秒格式
                    $callinfo_show_arr2['per_cal_time'] = floor($callinfo_show_arr2['cal_time']/$eff_cal_times2);//每通话电话时长:per_cal_time
                    $callinfo_show_arr2['per_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr2['per_cal_time']);//每通话电话时长:per_cal_time
                }
                foreach ($call_list3 as &$call) {  /*2呼叫转移入*/
                    $callinfo_show_arr3['dial_cal_time'] = 0;         //拨打电话：dial_cal_time
                    $callinfo_show_arr3['dial_cal_time_show'] = 0;
                    $callinfo_show_arr3['anser_cal_time'] += $call['calltime']; //累计接听电话:anser_cal_time
                    $callinfo_show_arr3['anser_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr3['anser_cal_time']); //累计接听电话:anser_cal_time转换为时分秒格式
                    $callinfo_show_arr3['cal_time'] += $call['calltime']; //通话时长:cal_time
                    $callinfo_show_arr3['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr3['cal_time']); //通话时长:cal_time转换为时分秒格式
                    $callinfo_show_arr3['per_cal_time'] = floor($callinfo_show_arr3['cal_time']/$eff_cal_times3);//每通话电话时长:per_cal_time
                    $callinfo_show_arr3['per_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr3['per_cal_time']);//每通话电话时长:per_cal_time
                }
                foreach ($call_list4 as &$call) {  /*3呼叫转移出*/
                    $callinfo_show_arr4['dial_cal_time'] += $call['calltime'];         //拨打电话：dial_cal_time
                    $callinfo_show_arr4['dial_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr4['dial_cal_time']); //累计接听电话:anser_cal_time转换为时分秒格式
                    $callinfo_show_arr4['anser_cal_time'] = 0; //累计接听电话:anser_cal_time
                    $callinfo_show_arr4['anser_cal_time_show'] = 0;
                    $callinfo_show_arr4['cal_time'] += $call['calltime']; //通话时长:cal_time
                    $callinfo_show_arr4['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr4['cal_time']); //通话时长:cal_time转换为时分秒格式
                    $callinfo_show_arr4['per_cal_time'] = floor($callinfo_show_arr4['cal_time']/$eff_cal_times4);//每通话电话时长:per_cal_time
                    $callinfo_show_arr4['per_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr4['per_cal_time']);//每通话电话时长:per_cal_time
                }
                foreach ($call_list5 as &$call) {  /*5双向外呼*/
                    $callinfo_show_arr5['dial_cal_time'] += $call['calltime'];         //拨打电话：dial_cal_time
                    $callinfo_show_arr5['dial_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr5['dial_cal_time']); //累计接听电话:anser_cal_time转换为时分秒格式
                    $callinfo_show_arr5['anser_cal_time'] = 0; //累计接听电话:anser_cal_time
                    $callinfo_show_arr5['anser_cal_time_show'] = 0;
                    $callinfo_show_arr5['cal_time'] += $call['calltime']; //通话时长:cal_time
                    $callinfo_show_arr5['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr5['cal_time']); //通话时长:cal_time转换为时分秒格式
                    $callinfo_show_arr5['per_cal_time'] = floor($callinfo_show_arr5['cal_time']/$eff_cal_times5);//每通话电话时长:per_cal_time
                    $callinfo_show_arr5['per_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr5['per_cal_time']);//每通话电话时长:per_cal_time
                }
                //拨打电话：dial_cal_time_show
                $callinfo_show_arr6['dial_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['dial_cal_time']+$callinfo_show_arr2['dial_cal_time']
                                                +$callinfo_show_arr3['dial_cal_time']+$callinfo_show_arr4['dial_cal_time']+$callinfo_show_arr5['dial_cal_time']);
                //累计接听电话:anser_cal_time_show
                $callinfo_show_arr6['anser_cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['anser_cal_time']+$callinfo_show_arr2['anser_cal_time']
                                                +$callinfo_show_arr3['anser_cal_time']+$callinfo_show_arr4['anser_cal_time']+$callinfo_show_arr5['anser_cal_time']);
                //通话时长：cal_time_show
                $callinfo_show_arr6['cal_time'] = $callinfo_show_arr1['cal_time']+$callinfo_show_arr2['cal_time']
                                                +$callinfo_show_arr3['cal_time']+$callinfo_show_arr4['cal_time']+$callinfo_show_arr5['cal_time'];
                $callinfo_show_arr6['cal_time_show'] = $MCalllog->Sec2Time($callinfo_show_arr1['cal_time']+$callinfo_show_arr2['cal_time']
                                                +$callinfo_show_arr3['cal_time']+$callinfo_show_arr4['cal_time']+$callinfo_show_arr5['cal_time']);
                $callinfo_show_arr6['per_cal_time_show'] = $MCalllog->Sec2Time(floor($callinfo_show_arr6['cal_time']/$eff_cal_times));//每通话电话时长:per_cal_time

        }
        //计算该时间段每天通话情况
            if($s_key == "week" && $s_begintime == '' && $s_endtime == ''){
                //本周时长统计
                foreach($week_show as &$days_week){
                    $map = $wheres;
                    $map['calldate'] = array('eq',$days_week);
                    $data_list[] = $MCalllog
                         ->field("sum(calltime) as calltime_sum,count(callrecord_id) as num,calldate")
                         ->where($map)
                         ->order("lasttime desc ")
                         ->select();
                }
                foreach($data_list as &$list_d){
                    if(!empty($list_d)){
                        foreach ($list_d as &$v) {
                             if($v['num']!= '0'){
                                $v['v_calltime_sum'] = $MCalllog->Sec2Time($v['calltime_sum']);
                                $v['v_calldate'] = substr($v['calldate'],0,4).'-'.substr($v['calldate'],4,2).'-'.substr($v['calldate'],6,2);
                             }
                        }
                    }
                }
                //本周时长统计
            }else if($s_key == "month" && $s_begintime == '' && $s_endtime == ''){
                foreach ($month_show as &$days) {
                    $map = $wheres;
                    $map['calldate'] = array('eq',$days);
                    $data_list[] = $MCalllog
                         ->field("sum(calltime) as calltime_sum,count(callrecord_id) as num,calldate")
                         ->where($map)
                         ->order("lasttime desc ")
                         ->select();
                }
                foreach($data_list as &$list_d){
                    if(!empty($list_d)){
                        foreach ($list_d as &$v) {
                             if($v['num']!= '0'){
                                $v['v_calltime_sum'] = $MCalllog->Sec2Time($v['calltime_sum']);
                                $v['v_calldate'] = substr($v['calldate'],6,2);
                             }
                        }
                    }
                }
            }
            //时间段时长统计
            if($s_begintime && $s_endtime && $s_begintime < $s_endtime){
                $day = round((strtotime($s_endtime)-strtotime($s_begintime))/3600/24)+1;     //该时间段共几天
                if($day <= 20){                         //时间间隔不超过20天，则显示正序每天数据
                    $i = -1;
                    for($j=strtotime($s_begintime); $j<=strtotime($s_endtime);$j+=86400) {
                        $time_show[]=date('Ymd',$j);
                        $i++;
                        $riqi = substr(strval(date('Ymd',$j)),6,2);
                        if($riqi < 10)  $time_show_2[$i]['vvv'] = substr(strval(date('Ymd',$j)),7,1);
                        else $time_show_2[$i]['vvv'] = $riqi;
                        $time_show_2[$i]['kkk'] = $i;
                    }
                }else{
                    $i = -1;
                    for($j=strtotime($s_begintime); $j<=strtotime($s_endtime);$j+=86400) {
                        $time_show[]=date('Ymd',$j);
                        $i++;
                        $riqi = substr(strval(date('Ymd',$j)),6,2);
                        if($riqi < 10)  $time_show_2[$i]['vvv'] = substr(strval(date('Ymd',$j)),7,1);  //日期小于10，只取一位，例2016-10-09截取9
                        else $time_show_2[$i]['vvv'] = $riqi;
                        $time_show_2[$i]['kkk'] = $i;
                    }
                    $time_show = array_reverse($time_show);   //时间间隔超过20天，则显示倒序每天数据
                    $time_show_2 = array_reverse($time_show_2);
                }

                $cnt = count($time_show);
                $Page = new \Common\ORG\Page( $cnt, $psize );

                foreach ($time_show as &$dayss) {
                    $maps = $wheres;
                    $maps['calldate'] = array('eq',$dayss);
                    $data_list[] = $MCalllog
                                     ->field("sum(calltime) as calltime_sum,count(callrecord_id) as num,calldate")
                                     ->where($maps)
                                     ->order("lasttime desc ")
                                     ->select();
                }
                foreach($data_list as &$list_d){
                    if(!empty($list_d)){
                        foreach ($list_d as &$v) {
                             if($v['num']!= '0'){
                                $v['v_calltime_sum'] = $MCalllog->Sec2Time($v['calltime_sum']);
                                $v['v_calldate'] = substr($v['calldate'],6,2);
                             }
                        }
                    }
                }
                $num = -1;
                if(($Page->nowPage)*$psize<=$cnt){
                    for($k=($Page->nowPage-1)*$psize;$k<($Page->nowPage)*$psize;$k++){
                        $num++;
                        $data_listss[$num] = $data_list[$k];
                        $time_show_2ss[$num] = $time_show_2[$k];
                    }
                }else{
                    for($k=($Page->nowPage-1)*$psize;$k<$cnt;$k++){
                        $num++;
                        $data_listss[$num] = $data_list[$k];
                        $time_show_2ss[$num] = $time_show_2[$k];
                    }
                }

            }
        if($cal_times){
            $res['type'] = $s_type;
            $res['key'] = $s_key;
            if($s_begintime && $s_endtime && $s_begintime < $s_endtime){
                $cnt = count($time_show);
                $res['call_list'] = $data_listss;
                $res['time_show_2'] = $time_show_2ss;           //获取时间段内每天时间，并只显示后两位
                $Page = new \Common\ORG\Page( $cnt, $psize );
                $res['pall'] =  $Page->getTotalPages();
                $res['p'] = $Page->nowPage;
                $res['psize'] = $psize;
            }else $res['call_list'] = $data_list;

            $res['cal_times'] = $cal_times;
            $res['eff_cal_times'] = $eff_cal_times;           //有效通话次数
            if($s_type == "1")      /*0打入*/
                $res['callinfo_show'] = $callinfo_show_arr1;
            else if($s_type == "2") /*1打出*/
                $res['callinfo_show'] = $callinfo_show_arr2;
            else if($s_type =="3") /*2呼叫转移入*/
                $res['callinfo_show'] = $callinfo_show_arr3;
            else if($s_type == "4") /*3呼叫转移出*/
                $res['callinfo_show'] = $callinfo_show_arr4;
            else if($s_type == "6") /*5双向外呼*/
                $res['callinfo_show'] = $callinfo_show_arr5;
            else $res['callinfo_show'] = $callinfo_show_arr6;  /*7全部*/
            $res['cnt'] = $cnt;         //时间段内的总天数
            $res['week_show'] = $week_show;                 //本周每天时间中不存在-
            $res['week_shows'] = $week_shows;               //本周每天时间中存在-，即20161012为2016-10-12
            $res['month_shows'] = $month_shows;             //获取本周起止时间(年月日之间没有-)
            $res['month_show'] = $month_show;               //获取本月每天的时间
            $res['month_show_2'] = $month_show_2;           //获取本月每天的时间，并只显示后两位
            $res['time_show'] = $time_show;               //获取时间段内每天时间
            $res['day'] = $day;                           //获取设置的时间段天数
            $res['rcode'] = 1;
        }else{
            $res['rcode'] = 2;
        }

        e_json( $res );

    }
    //获取通话记录
    public function calllogs(){
        $psize = I('get.psize', $this->m_psize,'intval');
        $s_id = I('get.id','','trim');
        $s_telephone = I('get.telephone','','trim');
        $s_begintime = I('get.begintime','','trim');
        $s_endtime = I('get.endtime','','trim');
        $s_type = I('get.type','', 'intval');

        if( !$psize )
            $psize = $this->m_psize;
        else if( $psize > 20 )
            $psize = $this->m_psize;

        $res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
        $MCalllog = D("Calllog");
        $wheres = array();
        $wheres['_string'] = '1=1';

        $wheres['admin_no'] = array('eq',$this->m_user['user_id']);
        if($s_id=="out")
            $wheres['type'] = array('eq',1);   //打出
        else if($s_id=="in")
            $wheres['type'] = array('eq',0);   //打入
        else if($s_id=="fal")
            $wheres['calltime'] = array('eq',0);   //未接电话
        if($s_telephone)
            $wheres['telephone'] = array('like','%'.$s_telephone.'%');
        if( $s_type && $s_type != "7" )
            $wheres['type'] = array('eq', $s_type-1 );
        else if($s_type && $s_type == "7")          //筛选通话类型为全部
            $wheres['type'] = array('in',array('0','1','2','3','4','5'));
        if($s_begintime && $s_endtime && $s_begintime < $s_endtime)                 //去掉日期间-(2016-10-09处理成20161009)
            $wheres['calldate'] = array('between',array(str_replace('-','',$s_begintime),str_replace('-','',$s_endtime)));
        else if($s_begintime && $s_endtime && strtotime($s_begintime) == strtotime($s_endtime))  //开始时间=结束时间则处理成该天通话信息
            $wheres['calldate'] = array('eq',str_replace('-','',$s_begintime));

        $cnt = $MCalllog->where( $wheres )->count();
        $Page = new \Common\ORG\Page( $cnt, $psize );
        $res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');

        $dlist = $MCalllog
                ->field("callrecord_id,call_id,calldate,ext_id,type,telephone,begintime,endtime,calltime,area,trunkNumber,channel,route,lasttime")
                ->where($wheres)
                ->order("lasttime desc ")
                ->limit($Page->firstRow.','.$Page->listRows )
                ->select();
        $sql = $MCalllog -> getLastSql();
        if($dlist){
            $types = $MCalllog->getTypes();
            $_types = array();
            foreach( $types as $v ){
                $_types[$v['k']] = $v['v'];
            }
            foreach( $dlist as &$vd){
                $vd['typename'] = isset( $_types[$vd['type']]) ?  $_types[$vd['type']] : $vd['type'];
                if($vd['calltime'])
                    $vd['v_calltime'] = $MCalllog->Sec2Time($vd['calltime']);    //通话时长转换为时分秒格式存入v_calltime
                else if($vd['calltime'] == 0)
                    $vd['v_calltime'] =0;
            }
            $res['rcode'] = 1;
            $res['data'] = $dlist;
            $res['pall'] =  $Page->getTotalPages();
            $res['p'] = $Page->nowPage;
            $res['psize'] = $psize;
            $res['where'] = $wheres;
            $res['sql'] = $sql;
        }else{
            $res['rcode'] = 2;
            $res['where'] = $wheres;
            $res['sql'] = $sql;
        }

    e_json($res);

    }

    /**
     * 根椐用户加载通话记录,分为未接电话、已拨电话、已接听电话
     *
     * @return [type] [description]
     */
	public function records(){
        $res =  array('rcode'=>1 ,'data'=>array(),'msg'=>'OK');
        $MLog = M('Calllog');
        $wherel = array();
        //未接电话 10条
        $wherel['admin_no'] = array('eq', $this->m_user['user_id']);
        $wherel['type'] = array('in',array(0,2));
        $wherel['calltime'] = array('eq', 0);
        $res['data']['missing'] = $MLog->field("callrecord_id,ext_id,area,telephone,begintime,calltime")
                                       ->where( $wherel )
                                       ->limit("10")
                                       ->order("lasttime desc")
                                       ->select();
        //已接听电话,20条
        $wherel['calltime'] = array('gt', 0);
        $res['data']['incoming'] = $MLog->field("callrecord_id,ext_id,area,telephone,begintime,calltime")
                                       ->where( $wherel )
                                       ->limit("10")
                                       ->order("lasttime desc")
                                       ->select();
        $wherel = array();
        $wherel['type'] = array('in', array(1,2,5));
        $res['data']['out'] = $MLog->field("callrecord_id,ext_id,area,telephone,begintime,calltime")
                                       ->where( $wherel )
                                       ->limit("10")
                                       ->order("lasttime desc")
                                       ->select();
        e_json( $res );
    }

}